# !/usr/bin/env python
# -*- coding:utf-8 -*-
'''用户配置

说明:
    保存当前用户配置

历史:
    2018-11-10 姚彧 创建
    2018-11-13 ... 加入机器相关前缀
'''

import os
import version
from tool.Singleton import MetaSingleton
from tool.AttributesTable import AttributesTable
import uuid


class UserConfig(object, metaclass = MetaSingleton):
    def __init__(self):
        config_file = os.path.join(version.AppEnviron.get_app_path(), 'user.db')
        self.table = AttributesTable(config_file)
        self.__machine_key = self.__get_machine_key()

    def get_value(self, key, default = None):
        return self.table.get_value(self.__get_user_key(key), default)

    def set_value(self, key, value):
        self.table.set_value(self.__get_user_key(key), value)

    @property
    def lastest_project(self):
        return self.get_value('lastest_project', '')

    @lastest_project.setter
    def lastest_project(self, value):
        return self.set_value('lastest_project', value)

    def __get_user_key(self, key):
        return '%s.%s' % (self.__machine_key, key)

    def __get_machine_key(self):
        return uuid.UUID(int = uuid.getnode()).hex[-12:].upper()
